<!-- 字典项页 -->
<template>
  <!-- 字典项搜索卡片 -->
  <el-card style="margin:20px;">
    <template #header>
      <el-form :inline="true">
        <el-form-item label="字典项名称" style="margin-top: 20px">
          <el-input placeholder="请输入字典项名称" style="width:220px;" v-model="searchForm.label"
          @keydown.enter="selectDictOption"></el-input>
          <el-input v-show="false"></el-input>
        </el-form-item>
        <el-button type="warning" style="float: right; margin-left: 15px; margin-top: 20px; margin-right: 5px"
                   @click="goBack">
          <el-icon style="margin-right: 5px"><Back /></el-icon>返回
        </el-button>
        <el-button style="float: right; margin-left: 15px; margin-top: 20px"
                   @click="resetSearch">
          <el-icon style="margin-right: 5px"><Refresh /></el-icon>重置
        </el-button>
        <el-button type="primary" style="float: right; margin-top: 20px"
                   @click="selectDictOption">
          <el-icon style="margin-right: 5px"><Search /></el-icon>查询
        </el-button>
      </el-form>
    </template>
    <div style="margin:10px 0px 40px 10px;">
      字典项列表
      <el-button type="primary" style="float: right;margin-top:-3px;"
                 @click="addDict" :disabled="check==false">
        <el-icon style="margin-right: 5px; margin-left: 0"><Plus /></el-icon>
        新增字典项
      </el-button>
    </div>
    <el-table :data="dictOptionArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="dictName" label="所属字典" align="center"/>
      <el-table-column prop="label" label="字典项名称" align="center"/>
      <el-table-column prop="value" label="字典项值" align="center"/>
      <el-table-column prop="sort" label="字典项排序" align="center"/>
      <el-table-column prop="remark" label="备注信息" align="center" width="320"/>
      <el-table-column prop="createTime" label="创建时间" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="editDictOption(scope.row.id)">编辑</el-button>
          <el-button type="primary" link @click="deleteDictOption(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 保存字典项弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;" :before-close="handleClose">
    <div style="height: 30px">
      <el-button type="warning" style="float: right; margin-left: 15px" @click="handleClose">
        <el-icon style="margin-right: 5px"><Back /></el-icon>返回
      </el-button>
      <el-button type="primary" style="float: right" @click="saveDictOption">
        <el-icon style="margin-right: 5px"><Collection /></el-icon>保存
      </el-button>
    </div>
    <hr>
    <el-form label-width="80px" label-position="top">
      <el-form-item label="字典项名称">
        <el-input placeholder="请输入字典项名称" v-model="saveDictOptionForm.label"></el-input>
      </el-form-item>
      <el-form-item label="字典项值">
        <el-input placeholder="请输入字典项值" v-model="saveDictOptionForm.value"></el-input>
      </el-form-item>
      <el-form-item label="字典项排序">
        <el-input placeholder="请输入字典项排序" v-model="saveDictOptionForm.sort"></el-input>
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input placeholder="请输入备注信息" v-model="saveDictOptionForm.remark"></el-input>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import router from "@/router";
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

//定义返回按钮触发的方法
const goBack = () => {
  router.push('/dict');
}
//检查url中是否有字典id
const check = new URLSearchParams(window.location.search).has('id')
//获取url中携带的字典id
const dictId = new URLSearchParams(window.location.search).get('id');

//定义对象保存字典项搜索条件
const searchForm = ref({label:'',dictId:dictId});
//定义数组保存字典项数据
const dictOptionArr = ref([]);
//定义查询字典项的方法
const selectDictOption = () => {
  let data = qs.stringify(searchForm.value);
  axios.get(BASE_URL+'/v1/dictOption/select?'+data).then((response)=>{
    if (response.data.code === 2000) {
      dictOptionArr.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg)
    }
  })
}
onMounted(() => {
  selectDictOption();
})
//定义重置搜索的方法
const resetSearch = () => {
  searchForm.value = {label:'',dictId:dictId};
  selectDictOption();
}

//定义变量保存“新增字典项”弹窗标题
const dialogTitle = ref('新增字典项');
//定义变量控制弹窗是否出现
const dialogVisible = ref(false);
//定义对象保存字典项表单数据
const saveDictOptionForm = ref({dictId:dictId, label:'', value:'', sort:'', remark:''});
//定义点击“新增字典项”触发
const addDict = () => {
  dialogTitle.value = '新增字典项';
  dialogVisible.value = true;
}
//定义弹窗关闭的方法
const handleClose = () => {
  if(confirm('您是否要关闭本弹窗?')){
    saveDictOptionForm.value = {dictId:dictId,label:'',value:'',sort:'',remark:''};
    dialogVisible.value = false;
  }
}
//定义保存字典项的方法
const saveDictOption = () => {
  let data = qs.stringify(saveDictOptionForm.value);
  console.log(data);
  axios.post(BASE_URL+'/v1/dictOption/save/',data).then((response)=>{
    if (response.data.code === 2000) {
      ElMessage.success('保存成功！');
      saveDictOptionForm.value = {dictId:dictId, label:'', value:'', sort:'', remark:''};
      dialogVisible.value = false;
      selectDictOption();
    } else {
      ElMessage.error(response.data.msg)
    }
  })
}

//定义编辑字典项的方法
const editDictOption = (id) => {
  dialogTitle.value = '编辑字典项';
  dialogVisible.value = true;
  axios.get(BASE_URL+'/v1/dictOption/select?id='+id).then((response)=>{
    if (response.data.code === 2000) {
      saveDictOptionForm.value = response.data.data[0];
    } else {
      ElMessage.error(response.data.msg)
    }
  })
}

//定义删除字典项的方法
const deleteDictOption = (id) => {
  if(confirm('您确定要删除该字典项吗？')){
    axios.post(BASE_URL+'/v1/dictOption/delete/'+id).then((response)=>{
      if (response.data.code === 2000) {
        ElMessage.success('删除成功！');
        selectDictOption();
      } else {
        ElMessage.error(response.data.msg)
      }
    })
  }
}
</script>

<style>

</style>